#!/bin/bash
########################################################################
# script name:  craete_user.sh
# Used for:     Syncing password everywhere
# Author:       Martin Dumont
# Contributors:
# Version:      0.1
#
# (date format: date +'%Y/%m/%d')
#
# Date          Version Contributors    Modifications
# ----------    ------- ------------    -------------
# 2011/04/11    0.1     MDUMONT         initial
# 2011/11/15    1.0     MDUMONT         changed logic around saprmt01
########################################################################
usage()
{
cat << STOP

usage: $0 [options] [ARG]

This script is used for ....

OPTIONS:
   -h               Comma seperated list of hosts, or ALL
   -u               Comma seperated list of user name
   -f               Force, don't ask any confirmation

Ex.:
$0 -h saprmt02 -u k0528817
$0 -h saprmt02,saprmt01 -u k0528817
$0 -h ALL -u k0528817

STOP
}
if [ `whoami` != root ]; then
   /bin/echo Please run this script as root or using sudo
   exit
fi
#######################################
# Get parameters
#######################################
while getopts fh:u: OPTION
do
        case $OPTION in
        h)
                HOSTS=$(/bin/echo $OPTARG | tr '[A-Z]' '[a-z]' | sed 's/,/ /g')
                ;;
        u)
                USERNAMELIST=$(/bin/echo $OPTARG | tr '[A-Z]' '[a-z]' | sed 's/,/ /g')
                ;;
        f)
                FORCE=1
                ;;
        \?)
                usage
                exit 1
                ;;
        *)
                usage
                exit 1
                ;;
        esac
done
if [ $OPTIND = 1 ]; then usage; exit 1; fi
if [ -z "$USERNAMELIST" ]; then /bin/echo "Specify a user..."; usage; exit 1; fi
#######################################
# Functions
#######################################
RAND=$RANDOM
DATAFILE=/tmp/create_user.data.$RAND
SCRIPTFILE=/tmp/create_user.script.$RAND
BAENV=$(dirname `which $0`)/loadenv.sh
if [ -f $BAENV ]; then . $BAENV; else /bin/echo "No env library ($BAENV)"; exit 1; fi
SSH_OPTS=" -x -q -o ConnectTimeout=15 -o BatchMode=yes -o StrictHostKeyChecking=no -o LogLevel=quiet"
#######################################
function askpassword {
	stty -echo
	read -p "Please enter new password: " PASSWORD
	/bin/echo
	read -p "Please enter again: " CHECKMATCH
	/bin/echo
	stty echo
	if [ "$CHECKMATCH" != "$PASSWORD" ]; then /bin/echo "\nPasswords are different, exiting."; exit 1; fi
	CRYPTPASSWORD=$(/bin/echo $PASSWORD | openssl passwd -stdin)
}
function ask_credential {
	#############
	# Username
	#############
	default="$USERNAME"
	read -p "Enter username [$default]: " -e a1
	if [ -n "$a1" ]; then USERNAME="$a1"; else USERNAME="$default"; fi
	#############
	# UID
	#############
	LASTUID=$(cat /etc/passwd | cut -d: -f3 | sort -n | grep '^4...$' | tail -1)
	if [ ! -n "$NEXTUID" ]; then NEXTUID=$(($LASTUID+1)); fi
	default="$NEXTUID"
	read -p "Enter UID [$default]: " -e a1
	if [ -n "$a1" ]; then NEXTUID="$a1"; else NEXTUID="$default"; fi
	#############
	# GID
	#############
	default="admin"
	read -p "Enter group [$default]: " -e a1
	if [ -n "$a1" ]; then GROUP="$a1"; else GROUP="$default"; fi
	#############
	# GECOS
	#############
	default="First_Last/email/[UNIX/SAP/ORA]"
	read -p "Enter GECOS [$default]: " -e a1
	if [ -n "$a1" ]; then USERGECOS="$a1"; else USERGECOS="$default"; fi
	#############
	# HOME
	#############
	default="/home/$USERNAME"
	read -p "Enter home dir [$default]: " -e a1
	if [ -n "$a1" ]; then USERHOME="$a1"; else USERHOME="$default"; fi
	#############
	# SHELL
	#############
	default="/usr/bin/ksh"
	read -p "Enter shell [$default]: " -e a1
	if [ -n "$a1" ]; then USERSHELL="$a1"; else USERSHELL="$default"; fi
	confirm_credential
}
function confirm_credential {
	if [ -z "$FORCE" ]
	then
		/bin/echo
		/bin/echo "Here are the defined values:"
		/bin/echo "Username = $USERNAME"
		/bin/echo "UID      = $NEXTUID"
		/bin/echo "GROUP    = $GROUP"
		/bin/echo "GECOS    = $USERGECOS"
		/bin/echo "HOME     = $USERHOME"
		/bin/echo "SHELL    = $USERSHELL"
		/bin/echo
		read -p "Is it ok? [y/n]: " -e a1
		/bin/echo
		if [ "$a1" != "y" ]
		then 
			ask_credential
		fi
	fi
	/bin/echo "$USERNAME:$NEXTUID:$GROUP:$USERGECOS:$USERHOME:$USERSHELL" >> $DATAFILE
}
function load_user_creds {
	if ! grep -q "^$USERNAME:" /etc/passwd
	then
		/bin/echo "User $USERNAME doesn't exist on $HOST, going interactive..."
		ask_credential
		if ! cat /etc/group | grep -q "^$GROUP" ; then /bin/echo "Group $GROUP doesn't exist"; exit 1; fi
		/bin/echo $USERNAME:!:$NEXTUID:$GROUP:$USERGECOS:$USERHOME:$USERSHELL >> /etc/passwd
	else
		LINE=$(grep -w "^$USERNAME:" /etc/passwd | head -1)
		NEXTUID=$(/bin/echo $LINE | cut -d":" -f3)
		GROUP=$(grep `/bin/echo $LINE | cut -d":" -f4` /etc/group | head -1 | cut -d: -f1)
		USERGECOS=$(/bin/echo $LINE | cut -d":" -f5)
		USERHOME=$(/bin/echo $LINE | cut -d":" -f6)
		USERSHELL=$(/bin/echo $LINE | cut -d":" -f7)
		confirm_credential
	fi
}
#######################################
# SCRIPTFILE
#######################################
NEWHOSTLIST=$(checkhost $HOSTS | /usr/bin/grep -v Verifying|tr ' ' '\n' | grep -v saprmt01)
askpassword
for USERNAME in $USERNAMELIST
do
	load_user_creds
done

while read LINE
do
	USERNAME=$(/bin/echo $LINE | cut -d":" -f1)
	NEXTUID=$(/bin/echo $LINE | cut -d":" -f2)
	GROUP=$(/bin/echo $LINE | cut -d":" -f3)
	USERGECOS=$(/bin/echo $LINE | cut -d":" -f4)
	USERHOME=$(/bin/echo $LINE | cut -d":" -f5)
	USERSHELL=$(/bin/echo $LINE | cut -d":" -f6)
	cat >> $SCRIPTFILE <<-EOF
	if uname | grep -q Linux; then OPT="-e"; else OPT=""; fi
	/bin/echo \$OPT "Creating user $USERNAME on \$(uname -n) \c"
EOF
	if ! /bin/echo $USERHOME | grep -q /home; then s_HOME=/tmp; else s_HOME=$USERHOME; fi
	cat >> $SCRIPTFILE <<-EOF
		/usr/sbin/useradd -c "$USERGECOS" -d $s_HOME -m -g $GROUP -u $NEXTUID -s $USERSHELL $USERNAME && /usr/sbin/usermod -d $USERHOME $USERNAME && /bin/echo ok
EOF
	cat >> $SCRIPTFILE <<-'EOF'
	OS=$(uname -s)
	if [ "$OS" = "AIX" ]
	then
EOF
	cat >> $SCRIPTFILE <<-EOF
	/bin/echo "$USERNAME:$CRYPTPASSWORD" | /usr/bin/chpasswd -ce
	else
EOF
	cat >> $SCRIPTFILE <<-EOF
	/bin/echo "$USERNAME:$PASSWORD" | /usr/sbin/chpasswd
	fi
EOF
done <$DATAFILE
#######################################
# SSH
#######################################
for HOSTNAME in $NEWHOSTLIST
do
	cat $SCRIPTFILE | sudo -u saunix ssh $SSH_OPTS $HOSTNAME "cat | sudo /bin/bash"
done
#######################################
exit 0
